public class Work1 {
    public static int count = 0;
    private final static Object locker = new Object();
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            synchronized (locker) {
                for (int i = 0; i < 10; i++) {
                    while (count % 3 != 0) {
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.print(Thread.currentThread().getName());
                    count++;
                    locker.notifyAll();
                }
            }

        },"A");
        Thread t2 = new Thread(() -> {
            synchronized (locker) {
                for (int i = 0; i < 10; i++) {
                    while (count % 3 != 1) {
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.print(Thread.currentThread().getName());
                    count++;
                    locker.notifyAll();
                }
            }

        },"B");
        Thread t3 = new Thread(() -> {
            synchronized (locker) {
                for (int i = 0; i < 10; i++) {
                    while (count % 3 != 2) {
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.print(Thread.currentThread().getName());
                    System.out.println();
                    count++;
                    locker.notifyAll();
                }
            }
        },"C");
        t1.start();
        t2.start();
        t3.start();
    }
}
